﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.Unity;

namespace UCenter.Core.Caching
{
    public abstract class CacheBase<TDependency> where TDependency : class
    {
        private readonly ICacheManager cacheManager;
        private List<TDependency> dependencies;

        public CacheBase()
        {
            dependencies = default(List<TDependency>);
        }


        [InjectionConstructor]
        public CacheBase([Dependency("UCenterCacheManager")]ICacheManager cacheManager)
            : this()
        {
            this.cacheManager = cacheManager;
        }

        protected ICacheManager CacheManager
        {
            get
            {
                return cacheManager;
            }
        }

        public void GetAllDependencies()
        {
            HttpContext context = HttpContext.Current;

            List<TDependency> list = ((IUnityContainer)context.Application["repositoryContainer"]).Resolve<IBusinessCache<TDependency>>().GetAllData();

            foreach (TDependency dependency in list)
            {
                dependencies.Add(dependency);
            }
        }

        public void Flush()
        {
            cacheManager.Flush();
        }
    }
}
